package com.jiyi.activity.datainput.sm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.SpaceLawBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.NoScrollGridView;
import com.jiyi.widget.picselect.PicsAdapter;
import com.jiyi.widget.picshow.ImagePagerActivity;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.RadioGroupUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/29.
 */

public class BuildSpaceLawReadActiviy extends ActivityBase {

    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.rbShouXuYes)
    RadioButton rbShouXuYes;
    @BindView(R.id.rbShouXuNo)
    RadioButton rbShouXuNo;
    @BindView(R.id.rgShouXuIsOK)
    RadioGroup rgShouXuIsOK;
    @BindView(R.id.tvShouXuProblem)
    TextView tvShouXuProblem;
    @BindView(R.id.tvShouXuMeasure)
    TextView tvShouXuMeasure;
    @BindView(R.id.rbSafeCheckYes)
    RadioButton rbSafeCheckYes;
    @BindView(R.id.rbSafeCheckNo)
    RadioButton rbSafeCheckNo;
    @BindView(R.id.rgSafeCheckIsOK)
    RadioGroup rgSafeCheckIsOK;
    @BindView(R.id.tvSafeCheckProblem)
    TextView tvSafeCheckProblem;
    @BindView(R.id.tvSafeCheckMeasure)
    TextView tvSafeCheckMeasure;
    @BindView(R.id.rbUseFuncYes)
    RadioButton rbUseFuncYes;
    @BindView(R.id.rbUseFuncNo)
    RadioButton rbUseFuncNo;
    @BindView(R.id.rgUseFuncIsOK)
    RadioGroup rgUseFuncIsOK;
    @BindView(R.id.tvUseFuncProblem)
    TextView tvUseFuncProblem;
    @BindView(R.id.tvUseFuncMeasure)
    TextView tvUseFuncMeasure;

    @BindView(R.id.gvShouXu)
    NoScrollGridView gvShouXu;
    @BindView(R.id.gvSafeCheck)
    NoScrollGridView gvSafeCheck;
    @BindView(R.id.gvUseFunc)
    NoScrollGridView gvUseFunc;

    String build_id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_build_space_law_read);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        getIntentData();
        setTop();
        setViewStatus();

        if (!TextUtils.isEmpty(build_id)) {
            //修改时，获取数据
            getData();
        }
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("建筑场所消防行政许可");
        tvSearch.setVisibility(View.GONE);
    }

    private void setViewStatus() {
        RadioGroupUtil.disableRadioGroup(rgShouXuIsOK);
        RadioGroupUtil.disableRadioGroup(rgSafeCheckIsOK);
        RadioGroupUtil.disableRadioGroup(rgUseFuncIsOK);
    }

    private void getData() {
        HttpMethods.getInstance().getSpaceLawInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<SpaceLawBean>(this) {
                    @Override
                    public void onNext(SpaceLawBean bean) {

                        if (bean == null || "".equals(bean.getGson_null())) {
                            bean = new SpaceLawBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(0);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            bean.setHandle_procedures(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(0);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            bean.setSecurity_check(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(0);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            bean.setChange_use(bc3);
                        }
                        fillData(bean);
                    }
                });
    }

    private void fillData(SpaceLawBean spaceLawBean) {
        if (spaceLawBean.getHandle_procedures().getIs_ok() == 1) {
            rbShouXuYes.setChecked(true);
        }

        tvShouXuProblem.setText(spaceLawBean.getHandle_procedures().getProblem());
        tvShouXuMeasure.setText(spaceLawBean.getHandle_procedures().getMeasures());

        if (spaceLawBean.getSecurity_check().getIs_ok() == 1) {
            rbSafeCheckYes.setChecked(true);
        }

        tvSafeCheckProblem.setText(spaceLawBean.getSecurity_check().getProblem());
        tvSafeCheckMeasure.setText(spaceLawBean.getSecurity_check().getMeasures());

        if (spaceLawBean.getChange_use().getIs_ok() == 1) {
            rbUseFuncYes.setChecked(true);
        }

        tvUseFuncProblem.setText(spaceLawBean.getChange_use().getProblem());
        tvUseFuncMeasure.setText(spaceLawBean.getChange_use().getMeasures());

        initGridViewList(spaceLawBean);

    }

    @OnClick({R.id.tvBack})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            default:
                break;
        }
    }

    void initGridViewList(SpaceLawBean spaceLawBean)
    {
        if (spaceLawBean.getHandle_procedures().getImage() == null || spaceLawBean.getHandle_procedures().getImage().size() <= 0) {
            gvShouXu.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvShouXu,spaceLawBean.getHandle_procedures().getImage());
        }


        if (spaceLawBean.getSecurity_check().getImage() == null || spaceLawBean.getSecurity_check().getImage().size() <= 0) {
            gvSafeCheck.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvSafeCheck,spaceLawBean.getSecurity_check().getImage());
        }

        if (spaceLawBean.getChange_use().getImage() == null || spaceLawBean.getChange_use().getImage().size() <= 0) {
            gvUseFunc.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvUseFunc,spaceLawBean.getChange_use().getImage());
        }
    }

    void setGridviewAdapter(GridView gridView,List<String> picList)
    {
        gridView.setVisibility(View.VISIBLE);
        //图片列表
        if (picList != null) {
            final String [] urls = new String [picList.size()];
            for (int i = 0; i < picList.size(); i++) {
                urls[i] = picList.get(i);
            }

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(view.getContext(),ImagePagerActivity.class);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                    view.getContext().startActivity(intent);
                }
            });
            PicsAdapter picsAdapter = new PicsAdapter(gridView.getContext(),picList);
            gridView.setAdapter(picsAdapter);
        }
    }

}
